/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * <p>Project: jse2302 - ArrSort1
 * <p>Powered by webrx On 2023-06-14 09:32:30
 * <p>Created by IntelliJ IDEA
 *
 * @author  webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class ArrSort1 {
    public static void main(String[] args) {
        //1 声明数组10个int元素
        int[] nums = new int[10];

        //2 给数组赋值1-100 随机整数
        Random rand = new Random();
        for (int i = 0; i < nums.length; i++) {
            nums[i] = rand.nextInt(1, 101);
        }

        //3 输出数组
        //System.out.println(nums);
        //for(var i : nums){
        //    System.out.printf("%d ",i);
        //}

        System.out.println(Arrays.toString(nums));

        //升序
        //Arrays.sort(nums);

        //冒泡排序算法
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums.length - 1 - i; j++) {
                if (nums[j] > nums[j + 1]) {
                    int t = nums[j];
                    nums[j] = nums[j + 1];
                    nums[j + 1] = t;
                }
            }
        }


        System.out.println(Arrays.toString(nums));

        //冒泡排序算法
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums.length - 1 - i; j++) {
                if (nums[j] < nums[j + 1]) {
                    int t = nums[j];
                    nums[j] = nums[j + 1];
                    nums[j + 1] = t;
                }
            }
        }
        System.out.println(Arrays.toString(nums));

        //洗牌乱序
        for (int i = 0; i < nums.length * 10; i++) {
            int x = rand.nextInt(nums.length);
            int y = rand.nextInt(nums.length);
            if (x == y) {
                --i;
                continue;
            }
            int t = nums[x];
            nums[x] = nums[y];
            nums[y] = t;
        }


        System.out.println(Arrays.toString(nums));
    }
}
